package me.alide.modules.wechart.cp.utils;


import me.alide.modules.wechart.cp.config.WxCpConfiguration;
import me.alide.modules.wechart.cp.config.WxCpProperties;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.article.NewArticle;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;

/**
 * @author wutairui
 * Created on 2020/4/8.
 */
public class SendMsgUtils {

    public  static  SendMsgUtils instance;
    public static SendMsgUtils getInstance() {
        if(instance == null) instance = new SendMsgUtils();
        return instance;
    }

    public void sendArticalMsg(WxCpProperties wxCpProperties, String url, String picUrl, String description, String title, String userId) {
        int agentId = wxCpProperties.getAppConfigs().get(0).getAgentId();
        WxCpService wxCpService = WxCpConfiguration.getCpService(agentId);
        NewArticle article1 = new NewArticle();
        article1.setUrl(url);
        article1.setPicUrl(picUrl);
        article1.setDescription(description);
        article1.setTitle(title);
        WxCpMessage wxCpMessage = WxCpMessage.NEWS()
                .agentId(agentId)
                .toUser(userId)
                .addArticle(article1)
                .build();
        try {
            wxCpService.getMessageService().send(wxCpMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }
}
